package cn.bytecollege.user.dao;

import cn.bytecollege.common.dao.BaseDao;
import cn.bytecollege.common.entity.Orders;

import java.time.LocalDateTime;
import java.util.List;


public class OrdersDao extends BaseDao<Orders,Integer> {
    /**
     * 根据用户Id查询出总订单
     * @param Id
     * @return
     */
    @Override
    public Orders findById(Integer Id) {
       String sql="select orders_id,user_id,create_time,address_id,product_sum_prices " +
               ",payment_state,order_number,payment_name from orders where user_id=?";
        List<Orders> lists=helper.executeQuery(sql,Orders.class,Id);
        Orders orders=lists.get(0);
       return orders;
    }
    /**
     * 创建总订单
     */
    @Override
    public int save(Orders orders) {
        String sql="INSERT INTO `transaction`.`orders`(`user_id`, `create_time`, `address_id` " +
                ", `product_sum_prices`, `payment_state`, `order_number`, `payment_name`) VALUES " +
                "( ?, ?, ?, ?, ?, ?, ?);";
        Object[] objects={orders.getUserId(), LocalDateTime.now(),orders.getAddressId(),
         orders.getProductSumPrices(),orders.getPaymentState(),orders.getOrderNumber(),orders.getPaymentName()};
        return helper.executeUpdate(sql,objects);
    }
}
